package map;

import java.util.HashMap;
import java.util.Map;

/**
 * 1170. 比较字符串最小字母出现频次
 */
public class NumSmallerByFrequency {
    public int[] numSmallerByFrequency(String[] queries, String[] words) {
        int[] ret = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int qCount = f(queries[i]);
            int count = 0;
            for (int j = 0; j < words.length; j++) {
                int wCount = f(words[j]);
                if (qCount < wCount) {
                    count ++;
                }
            }
            ret[i] = count;
        }
        return ret;
    }

    /**
     * 统计str最小字母的出现次数
     * @param str
     * @return
     */
    public int f(String str) {
        Map<Character, Integer> map = new HashMap<>();
        // 'a'10 'z'35
        char minChar = 'z' + '1';
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
            if (c < minChar) {
                minChar = c;
            }
        }
        return map.get(minChar);
    }
}
